package com.spontaneappnew.fragments;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.spontaneappnew.Place;
import com.spontaneappnew.Places;
import com.spontaneappnew.R;
import com.spontaneappnew.activities.PlaceProfileActivity;

public class PlacesByDateFragment extends AllPlacesFragment {

	// State
	private ArrayList<Place> placesByDate;
	private static PlaceAdapter adapter;
	private static ListView placeList;
	
	public PlacesByDateFragment(){}
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    	sortPlacesByDate();
    	adapter = new PlaceAdapter(getActivity(), placesByDate);
    	placeList = (ListView)rootView.findViewById(R.id.home_fragment_place_listview);
    	placeList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    	placeList.setEmptyView(rootView.findViewById(R.id.empty_place_list));
		placeList.setAdapter(adapter);
		
		placeList.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(parent.getContext(), PlaceProfileActivity.class);
				ArrayList<Place> allPlaces = Places.getInstance().getAllPlaces();
				int realIndex = -1;
				boolean isFound = false;
				if(placesByDate.get(position) != null){
					for(int i = 0; !isFound && i < placesByDate.size(); i++){
						if(allPlaces.get(i).getId().compareTo(placesByDate.get(position).getId()) == 0){
							isFound = true;
							realIndex = i;
						}
					}
				}
				if(realIndex != -1){
					Places.getInstance().setCurrentPlace(realIndex, 1);
				    startActivity(intent);
				    getActivity().overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
				} else {
					Toast.makeText(getActivity(), "Something went wrong, please reload", Toast.LENGTH_SHORT).show();
				}
			}
		});
        return rootView;
    }
    
    @Override
	public void onResume() {
		super.onResume();
		if(placesByDate.size() != Places.getInstance().getAllPlaces().size()){
			sortPlacesByDate();
			adapter.notifyDataSetChanged();
			listHandler.notifyPagerAdapterDataSetChanged();
		}
	}

	/*
     * Do a QUICK SORT on the places list - by date descending
     */
    private void sortPlacesByDate() {
    	placesByDate = new ArrayList<Place>(Places.getInstance().getAllPlaces());
    	int numOfPlaces = placesByDate.size();
    	// if the Places list is empty, return
    	if(numOfPlaces == 0){
    		return;
    	}
    	// place list not empty, do quick sort
    	else{
    		quickSort(0, numOfPlaces-1);
    	}
	}

	private void quickSort(int low, int high) {
		int i = low, j = high;
	    Place pivot = placesByDate.get(low + (high-low)/2);
	    
	    while (i <= j) {
	    	while (placesByDate.get(i).getDate().after(pivot.getDate())) {
	    		i++;
	    	}
	    	while (placesByDate.get(j).getDate().before(pivot.getDate())) {
	    		j--;
	    	}
	    	if (i <= j) {
	    		swap(i, j);
	    		i++;
	    		j--;
	    	}
	    }
	    // Recursion
	    if (low < j)
	      quickSort(low, j);
	    if (i < high)
	      quickSort(i, high);
	}

	private void swap(int i, int j) {
		Place temp = placesByDate.get(i);
		placesByDate.set(i, placesByDate.get(j));
		placesByDate.set(j, temp);
	}
}